
package com.cj.bus.config;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
import com.cj.bus.common.utils.SnowflakeUtils;

/**
 * 字段自动填充
 * @TableField(fill = FieldFill.XXXX)
 * 
 * @author c-xiaomr
 *
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		 TableInfo tableInfo = TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass());
		 if(tableInfo.getIdType() == IdType.ID_WORKER_STR) {
			 this.setFieldValByName(tableInfo.getKeyProperty(), SnowflakeUtils.getSerialNumWithDate(), metaObject);
		 }
		 //this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
		 //this.setFieldValByName("updateTime", (int)(System.currentTimeMillis()/1000), metaObject);
		 //this.setFieldValByName("ifDeleted", 0, metaObject);
		 
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		// this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
	}
}
